import React, {useReducer} from 'react'

function countReducer(state, action) {
  switch (action.type) {
    case 'add':
      return state + 1;
    case 'sub':
      return state - 1;
    case 'mul':
      return state * 2;
    case 'def':
      return 0;
    default:
      return state;
  }
}

export default function UseReducer() {
  const [count, dispatch] = useReducer(countReducer, 0);
  return (<>
    <p>count : {count}</p>
    <button className='btn' onClick={() => dispatch({type: 'add'})}>add</button>
    <button className='btn' onClick={() => dispatch({type: 'sub'})}>sub</button>
    <button className='btn' onClick={() => dispatch({type: 'mul'})}>x2</button>
    <button className='btn' onClick={() => dispatch({type: 'def'})}>def</button>
  </>)
}
